﻿using Plugins;
using Utils.Wpf;

namespace FullDayStrategyPlugin
{
	internal class StrategyControlVM : ViewModelValidatedBase
	{
		#region Properties

		public StrategyControlData Data { get; private set; }

		public ExecutionDirection ExecutionDirection
		{
			get
			{
				return this.Data.ExecutionDirection;
			}
			set
			{
				if (this.Data.ExecutionDirection != value)
				{
					this.Data.ExecutionDirection = value;
					base.OnPropertyChanged("ExecutionDirection");
				}
			}
		}

		public decimal StopLoss
		{
			get
			{
				return this.Data.StopLoss;
			}
			set
			{
				if (this.Data.StopLoss != value)
				{
					this.Data.StopLoss = value;
					base.OnPropertyChanged("StopLoss");
				}
			}
		}

		public int VolumeIncrement
		{
			get
			{
				return this.Data.VolumeIncrement;
			}
			set
			{
				if (this.Data.VolumeIncrement != value)
				{
					this.Data.VolumeIncrement = value;
					base.OnPropertyChanged("VolumeIncrement");
				}
			}
		}

		public decimal ResetThreshold
		{
			get
			{
				return this.Data.ResetThreshold;
			}
			set
			{
				if (this.Data.ResetThreshold != value)
				{
					this.Data.ResetThreshold = value;
					base.OnPropertyChanged("ResetThreshold");
				}
			}
		}

		public VolumeMode VolumeMode
		{
			get
			{
				return this.Data.VolumeMode;
			}
			set
			{
				if (this.Data.VolumeMode != value)
				{
					this.Data.VolumeMode = value;
					base.OnPropertyChanged("VolumeMode");
				}
			}
		}

		public bool InvertLogic
		{
			get
			{
				return this.Data.InvertLogic;
			}
			set
			{
				if (this.Data.InvertLogic != value)
				{
					this.Data.InvertLogic = value;
					base.OnPropertyChanged("InvertLogic");
				}
			}
		}

		public bool StopLossAt0
		{
			get
			{
				return this.Data.StopLossAt0;
			}
			set
			{
				if (this.Data.StopLossAt0 != value)
				{
					this.Data.StopLossAt0 = value;
					base.OnPropertyChanged("StopLossAt0");
				}
			}
		}

		public bool MultipleOperations
		{
			get
			{
				return this.Data.MultipleOperations;
			}
			set
			{
				if (this.Data.MultipleOperations != value)
				{
					this.Data.MultipleOperations = value;
					base.OnPropertyChanged("MultipleOperations");
				}
			}
		}

		public bool DynamicStop
		{
			get
			{
				return this.Data.DynamicStop;
			}
			set
			{
				if (this.Data.DynamicStop != value)
				{
					this.Data.DynamicStop = value;
					base.OnPropertyChanged("DynamicStop");
				}
			}
		}

		public int QuantityToBuy
		{
			get
			{
				return this.Data.QuantityToBuy;
			}
			set
			{
				if (this.Data.QuantityToBuy != value)
				{
					this.Data.QuantityToBuy = value;
					base.OnPropertyChanged("QuantityToBuy");
				}
			}
		}

		public int MaxVolume
		{
			get
			{
				return this.Data.MaxVolume;
			}
			set
			{
				if (this.Data.MaxVolume != value)
				{
					this.Data.MaxVolume = value;
					base.OnPropertyChanged("MaxVolume");
				}
			}
		}

		public decimal MaxInverseValue
		{
			get
			{
				return this.Data.MaxInverseValue;
			}
			set
			{
				if (this.Data.MaxInverseValue != value)
				{
					this.Data.MaxInverseValue = value;
					base.OnPropertyChanged("MaxVolume");
				}
			}
		}

		public decimal MaxProfit
		{
			get
			{
				return this.Data.MaxProfit;
			}
			set
			{
				if (this.Data.MaxProfit != value)
				{
					this.Data.MaxProfit = value;
					base.OnPropertyChanged("MaxProfit");
				}
			}
		}

		#endregion

		#region Public Methods

		public StrategyControlVM()
		{
			this.Data = new StrategyControlData();
			this.Data.PropertyChanged += Data_PropertyChanged; // Local object, do not need dispose
		}

		protected override string ValidateField(string fieldName)
		{
			string result = null;

			if (fieldName == "QuantityToBuy")
			{
				if (this.QuantityToBuy < 1)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The quantity to buy has to be greater than 0";
				}
			}
			else if (fieldName == "VolumeIncrement")
			{
				if (this.VolumeIncrement < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The volume increment cannot be smaller than one";
				}
			}
			else if (fieldName == "StopLoss")
			{
				if (this.StopLoss < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The stop loss cannot be smaller than one";
				}
			}
			else if (fieldName == "ResetThreshold")
			{
				if (this.ResetThreshold < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The reset threshold cannot be smaller than zero";
				}
			}
			else if (fieldName == "MaxVolume")
			{
				if (this.MaxVolume < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The max volume cannot be smaller than zero";
				}
			}
			else if (fieldName == "MaxInverseValue")
			{
				if (this.MaxInverseValue < -1)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The max volume cannot be smaller than zero";
				}
			}

			return result;
		}

		#endregion

		#region Event Handlers

		private void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e.PropertyName);
		}

		#endregion
	}
}
